const fs = require("fs");
const path = require("path");
const processMain = require("process");

// 绝对路径
const absolutePath = path.resolve();

if (processMain.argv[2] === "view") {
  fs.mkdir(
    `${absolutePath}/src/view/${processMain.argv[3]}`,
    { recursive: true },
    (error) => {
      if (error) {
        console.log(`view目录创建失败`);
      }
    }
  );

  // 生成vue文件
  const view = fs.createWriteStream(
    `${absolutePath}/src/views/${processMain.argv[3]}/index.vue`
  );
  view.write(modelGenerator(processMain.argv[3]));
  view.end();

  // 生成router文件
  const router = fs.createWriteStream(
    `${absolutePath}/src/router/modules/${processMain.argv[3]}.js`
  );
  router.write(viewGenerator(processMain.argv[3]));
  router.end();

  // 生成store文件
  const store = fs.createWriteStream(
    `${absolutePath}/src/store/modules/${processMain.argv[3]}.js`
  );
  store.write(viewGenerator(processMain.argv[3]));
  store.end();

  // 生成api文件
  const api = fs.createWriteStream(
    `${absolutePath}/src/api/${processMain.argv[3]}.js`
  );
  api.write(viewGenerator(processMain.argv[3]));
  api.end();
} else {
  fs.mkdir(
    `${absolutePath}/src/cpmponents/${processMain.argv[3]}`,
    { recursive: true },
    (error) => {
      if (error) {
        console.log(`cpmponents目录创建失败`);
      }
    }
  );

  // 生成components文件
  const component = fs.createWriteStream(
    `${absolutePath}/src/cpmponents/${processMain.argv[3]}/index.vue`
  );
  component.write(modelGenerator(processMain.argv[3]));
  component.end();
}
